if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}

$(function () {
    if (AGENT_CLIENT.WEB) {
        ready();
    }
})

function ready() {

    var _user = null;
    var _thisPage = null;
    var html = [];
    var _passType = "";
    var _phone = "";
    var _backurl = "";
    var requestUrl = CONFIG_MANAGER.RequstUrl;


    var validatePage = function () {
        this.init();
    };

    validatePage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            _user = getLocalStorage(STORAGE_MANAGER.user);
            if (!_user) {
                redirect("../account/login.html");
                return;
            }

            _passType = getUrlParam("type");
            _phone = getUrlParam("phone");
            _backurl = getUrlParam("backurl");
            $("#phone").html(_phone).attr("tip", _user.id);
            if('login' == _passType){
            	$("#passwordType").html('修改登录密码');
            }else{
            	$("#passwordType").html('修改支付密码');
            }
        },
        bindEvent: function () {
            //回退
            $("#back").on("click", function () {
                back();
            });

            //点击获取验证码
            $("#getVcode").on("click", function () {
                change_code(this, $("#phone"), SMS_TYPE.safety);
            });
            
            $("#vcode,#nPassword,#confirmNpassword").on("input", function(){
            	if("" != $("#vcode").val() && 
            		 "" != $("#nPassword").val() &&
            	   "" != $("#confirmNpassword").val()){
            	   	$("#confirm").addClass("liang");
          	  }else{
          	   	$("#confirm").removeClass("liang");
          	  }
            });

            //点击下一步
            $("#confirm").on("click", function () {
                //验证码
                if ('' == $("#vcode").val().trim()) {
                    $.toast("请填写验证码");
                    return false;
                } 
								if (!isVCode($("#vcode").val().trim())) {
                    $.toast("验证码格式不正确");
                    return false;
                }
                if ("" == $("#nPassword").val()) {
                    $.toast("请填写新密码");
                    return false;
                }
                if ("" == $("#confirmNpassword").val()) {
                    $.toast("请再次填写新原密码");
                    return false;
                }
                if('pay' == _passType){
	                if (!isPasswd($("#nPassword").val())) {
	                    $.toast("密码为6至15位数字字母或下划线");
	                    return false;
	                }
                }
                if ( $("#confirmNpassword").val() != $("#nPassword").val() ) {
                    $.toast("两次输入的密码不一致");
                    return false;
                }
                var vcode = $("#vcode").val();
                
                switch (_passType) {
                  case "login":
                      requestUrl = CONFIG_MANAGER.RequstUrl + "v1/member/safety/loginpasswd/";
                      break;
                  case "pay":
                      requestUrl = CONFIG_MANAGER.RequstUrl + "v1/member/safety/paypasswd/";
                      break;
                  default:
                      break;
                }
                var postData = {"id": _user.id, "vcode": vcode, "passwd": $("#confirmNpassword").val()};
                $.ajax({
                  type: "PUT",
                  url: requestUrl,
                  async: true,
                  data: postData,
                  headers: {"token": _user.token},
                  beforeSend: function(){ $.showWaiting(); }
                }).done(function (data) {
                  $.alert("修改成功", function () {
                  	if(_backurl){
                  		var redirectUrl = "../" + _backurl.replace("|", "/");
                  		redirect(redirectUrl);
                  	}else{
                  		redirect("account.html");
                  	}
                    return false;
                  });
                }).always(function () {
                	$.closeWaiting();
                }).fail(function (data) {
                    $.toast(data.responseJSON.Message);
                });

////跳转
//var redirectUrl = "editPass.html?type=" + _passType + "&vcode=" + $("#vcode").val().trim();
//if (_backurl) {
//	redirectUrl += "&backurl=" + _backurl;
//}
//redirect(redirectUrl);

            });
        }
    }
    _thisPage = new validatePage();
}
